package com.thrd.basicframework.utils;

import android.util.Log;


public class LogUtil {
    public static final String TAG = "DEBUG";
    public static final String TEST = "RUNDE_TEST";

    public static boolean isDebug = true;

    public static void test(Object msg) {
        if (!isDebug) {
            return;
        }
        if (msg != null) {
            Log.e(TEST, getStackInfo() + msg.toString());
        } else {
            Log.e(TEST, "null");
        }
    }

    public static void test(String tag, Object msg) {
        if (!isDebug) {
            return;
        }
        if (msg != null) {
            Log.i(tag, getStackInfo() + msg.toString());
        } else {
            Log.i(tag, "null");
        }
    }
    public static void i(String tag, Object msg) {
        if (!isDebug) {
            return;
        }
        if (msg != null) {
            Log.i(tag, getStackInfo() + msg.toString());
        } else {
            Log.i(tag, "null");
        }
    }
    public static void i( Object msg) {
        if (!isDebug) {
            return;
        }
        if (msg != null) {
            Log.i(TEST, getStackInfo() + msg.toString());
        } else {
            Log.i(TEST, "null");
        }
    }


    public static void d(String tag, Object msg) {
        if (!isDebug) {
            return;
        }
        if (msg != null) {
            Log.d(TEST, tag+getStackInfo() + msg.toString());
        } else {
            Log.d(TEST, tag+ "null");
        }
    }

    public static void e(Object msg) {
        if (!isDebug) {
            return;
        }
        if (msg != null) {
            Log.e(TEST, getStackInfo() + msg.toString());
        } else {
            Log.e(TEST, getStackInfo() + "null");
        }
    }

    public static void e(String tag, Object msg) {
        if (!isDebug) {
            return;
        }
        if (msg != null) {
            Log.e(tag, getStackInfo() + msg.toString());
        } else {
            Log.e(tag, "null");
        }
    }

//    public static void ej(Object obj) {
//        if (!isDebug) {
//            return;
//        }
//        String msg = new Gson().toJson(obj);
//        if (msg != null) {
//            Log.e("TEST", getStackInfo() + msg);
//        } else {
//            Log.e("TEST", "null");
//        }
//    }

//    public static void eF(Object obj) {
//        StringBuilder sb = new StringBuilder();
//        StackTraceElement[] elements = Thread.getAllStackTraces().get(Thread.currentThread());
//        if (elements != null) {
//            for (StackTraceElement element : elements) {
//                String[] structs = element.getClassName().split("\\.");
//                sb.append(String.format("%s (%s.java:%s)\n", element.getClassName(), structs[structs.length - 1], element.getLineNumber()));
//            }
//        }
//        if (obj == null) {
//            sb.append("null");
//        } else {
//            sb.append(obj.getClass().getSimpleName());
//            sb.append("    ");
//            sb.append(new JSON.toJSON(obj));
//        }
//        Log.e(TEST, sb.toString());
//    }

    private static String getStackInfo() {
        return getStackInfo(6);
    }

    private static String getStackInfo(int lines) {
        StackTraceElement[] elements = Thread.getAllStackTraces().get(Thread.currentThread());
        String info = "";
        if (elements != null && elements.length > lines + 1) {
            StackTraceElement element = elements[lines];
            String[] structs = element.getClassName().split("\\.");
            String className = structs[structs.length - 1];
            className = className.split("\\$")[0];
            info += String.format("%s (%s.java:%s)\n", element.getClassName(), className, element.getLineNumber());
        }
        return info;
    }
}
